package com.allwees.bs.module.ops.listener;

import com.allwees.bs.core.model.constant.CacheName;
import com.allwees.bs.core.model.ops.AdsModel;
import com.allwees.bs.core.model.ops.event.AdUpdatedEvent;
import com.allwees.bs.module.ops.service.AdsService;
import com.allwees.core.cache.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.utils.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

import javax.annotation.Resource;

@Slf4j
@Component
public class AdUpdatedEventListener {

	@Resource
	private AdsService adsService;

	@TransactionalEventListener
	public void onEvent(AdUpdatedEvent event) {
		Integer id = (Integer) event.getSource();
		AdsModel model = adsService.getById(id);
		if(model != null){
			String key;
			String channel = model.getChannelUuid();
			int position = model.getPosition().getCode();
			String platform = model.getPlatform().name();
			if (StringUtils.isBlank(channel)) {
				key = String.format("%s::%s-%s", CacheName.OPS_ADS, position, platform);
			} else {
				key = String.format("%s::%s-%s-%s", CacheName.OPS_ADS, position, platform,channel);
			}
			log.info(">>> update advertisement cache,key:{}",key);
			RedisUtil.del(key);
		}
	}
}
